package data.news;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class Article {
	
	/** every entry in the table has 9 field */
	public static String  FLD_ID        = "id";

	public static Pattern deleteLinePattern = Pattern.compile("<.*?> *");
	public static Pattern deleteTagPattern  = Pattern.compile("/[A-Z\\p{Punct}]+");
	public static Pattern doubleLinePattern = Pattern.compile("(\n)\n");
	public static Pattern wordCntPattern    = Pattern.compile(".\\w+/[A-Z]+");
	public static Pattern goLeftPattern     = Pattern.compile(" (,|\\.|\\)|;|\\?|!|'|:)");
	public static Pattern stopPattern       = Pattern.compile("(\\w\\p{Punct}+)\n( (\\S))");
	
	public static String replaceAllWithGroups(Pattern pattern, String input, int...group){
		Matcher matcher = pattern.matcher(input);
		boolean b = matcher.find();
	    if (b) {
	    	StringBuffer sb = new StringBuffer();
	    	StringBuffer replacement = new StringBuffer();
	    	do {
	    		if (group == null) {
	    			matcher.appendReplacement(sb, "");
				}
	    		else if (group.length==1) {
	    			matcher.appendReplacement(sb, matcher.group(group[0]));	
				}
	    		else{
	    			replacement.delete(0, replacement.length());
	    			for (int i : group) {
						replacement.append(matcher.group(i));
					}
	    			matcher.appendReplacement(sb, replacement.toString().replace("$", "\\$"));
	    		}
	    		b = matcher.find();
	    	} while (b);
	    	matcher.appendTail(sb);
	    	return sb.toString();
	    }
		return input;
	}
	
	public KeywordIterator keywords(String... tagNames){
		KeywordIterator iter = new KeywordIterator(getContent(), tagNames);
		iter.setCurrentArticleID(this.getArticleID());
		return iter;
	}
	public long getArticleID() {
		return articleID;
	}
	public void setArticleID(long articleID) {
		this.articleID = articleID;
	}
	public String getContent() {
		return content;
	}
	public static String getCleanContent(String content){
		String result = null;
		result = replaceAllWithGroups(deleteTagPattern,  content, null);
		result = replaceAllWithGroups(deleteLinePattern, result,  null);
		result = replaceAllWithGroups(doubleLinePattern, result,  1);
		result = replaceAllWithGroups(goLeftPattern,     result,  1);
		result = replaceAllWithGroups(stopPattern,       result,  new int[]{1,2});
		result = result.replace("$ ", "$");
		result = result.replace("( ", "(");
		result = result.replace("`` ", "``");
		result = result.replace(" n't", "n't");
		return result;	
	}
	public int getSentenceCount(){
		BufferedReader br = new BufferedReader(new StringReader(getContent()));
		int lineCnt = 0;
		String line = null;
		try {
			while((line = br.readLine())!=null){
				if (line.length()<8) {
					continue;
				}
				lineCnt++;
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return lineCnt;
	}
	public String getCleanContent(){
		return getCleanContent(content);
	}
	public int wordCount(){
		Matcher matcher = wordCntPattern.matcher(content);
		int idx = 0;
		int result = 0;
		while(matcher.find(idx)){
			idx = matcher.end(0);
			System.out.println(matcher.group(0));
			result ++;
		}
		return result;
	}

	
	public void setContent(String content) {
		this.content = content;
	}
	public String getUrl() {
		return url;
	}
	public void setUrl(String url) {
		this.url = url;
	}
	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public int getDate() {
		return date;
	}
	public void setDate(int date) {
		this.date = date;
	}
	public String getSourcename() {
		return sourcename;
	}
	public void setSourcename(String sourcename) {
		this.sourcename = sourcename;
	}
	public String getAuthor() {
		return author;
	}
	public void setAuthor(String author) {
		this.author = author;
	}
	public String getArticletype() {
		return articletype;
	}
	public void setArticletype(String articletype) {
		this.articletype = articletype;
	}
	public String getSyndicate() {
		return syndicate;
	}
	public void setSyndicate(String syndicate) {
		this.syndicate = syndicate;
	}
	public String getPredictedarticletype() {
		return predictedarticletype;
	}
	public void setPredictedarticletype(String predictedarticletype) {
		this.predictedarticletype = predictedarticletype;
	}
	private long articleID = -1;
	private String content = null;
	private String url = null;
	private String title = null;
	private int date = -1;
	private String sourcename = null;
	private String author = null;
	private String articletype = null;
	private String syndicate = null;
	private String predictedarticletype = null;
	public Article(long id) {
		this.articleID = id;
	}
}
